import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import 'package:intl/intl.dart';

// void main() => runApp(MyApp());
//
// class MyApp extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     return MaterialApp(
//       title: '会议详情',
//       theme: ThemeData(
//         primarySwatch: Colors.blue,
//         visualDensity: VisualDensity.adaptivePlatformDensity,
//       ),
//       home: MeetingDetailPage(),
//     );
//   }
// }

class MeetingDetailPage extends StatefulWidget {
  @override
  _MeetingDetailPageState createState() => _MeetingDetailPageState();
}

class _MeetingDetailPageState extends State<MeetingDetailPage> {
  // 模拟会议数据
  final Map<String, dynamic> _meetingData = {
    'title': 'Q2产品规划会议',
    'status': '进行中',
    'time': {
      'start': DateTime(2024, 3, 15, 14, 0),
      'end': DateTime(2024, 3, 15, 16, 30)
    },
    'location': '18楼会议室A',
    'organizer': '张伟',
    'participants': [
      {'name': '李明', 'avatar': 'assets/user1.png', 'status': 'accepted'},
      {'name': '王芳', 'avatar': 'assets/user2.png', 'status': 'pending'},
      {'name': '陈强', 'avatar': 'assets/user3.png', 'status': 'declined'},
      {'name': '刘洋', 'avatar': 'assets/user4.png', 'status': 'accepted'},
    ],
    'agenda': [
      {'time': '14:00-14:30', 'topic': '市场趋势分析'},
      {'time': '14:30-15:15', 'topic': '产品路线图讨论'},
      {'time': '15:30-16:15', 'topic': '资源分配方案'},
      {'time': '16:15-16:30', 'topic': '下一步行动计划'},
    ],
    'attachments': [
      {'name': '产品规划草案.pdf', 'size': '3.2MB', 'type': 'pdf'},
      {'name': '市场数据报告.pptx', 'size': '5.1MB', 'type': 'ppt'},
    ]
  };

  Widget _buildStatusIcon(String status) {
    Map<String, IconData> icons = {
      'accepted': Icons.check_circle,
      'pending': Icons.help_outline,
      'declined': Icons.cancel
    };

    Map<String, Color> colors = {
      'accepted': Colors.green,
      'pending': Colors.orange,
      'declined': Colors.red
    };

    return Icon(
      icons[status],
      color: colors[status],
      size: 16,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('会议详情'),
        actions: [
          IconButton(
            icon: Icon(Icons.more_vert),
            onPressed: () {},
          )
        ],
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 会议标题和状态
            Row(
              children: [
                Expanded(
                  child: Text(
                    _meetingData['title'],
                    style: TextStyle(
                      fontSize: 22,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
                Chip(
                  label: Text(_meetingData['status']),
                  backgroundColor: Colors.blue[100],
                  labelStyle: TextStyle(color: Colors.blue[800]),
                )
              ],
            ),
            SizedBox(height: 20),

            // 时间和地点
            _buildDetailCard(
              icon: Icons.access_time,
              title: '会议时间',
              content: DateFormat('yyyy-MM-dd HH:mm').format(_meetingData['time']['start']) +
                  '\n至 ' +
                  DateFormat('HH:mm').format(_meetingData['time']['end']),
              trailing: Text(_meetingData['location']),
            ),

            // 组织者
            _buildDetailCard(
              icon: Icons.person,
              title: '组织者',
              content: _meetingData['organizer'],
            ),

            // 参与人员
            Card(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('参与人员', style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16
                    )),
                    SizedBox(height: 12),
                    Wrap(
                      spacing: 12,
                      runSpacing: 12,
                      children: _meetingData['participants'].map<Widget>((p) {
                        return Stack(
                          children: [
                            CircleAvatar(
                              radius: 24,
                              backgroundImage: AssetImage(p['avatar']),
                            ),
                            Positioned(
                              right: 0,
                              bottom: 0,
                              child: _buildStatusIcon(p['status']),
                            )
                          ],
                        );
                      }).toList(),
                    ),
                  ],
                ),
              ),
            ),

            // 会议议程
            Card(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('会议议程', style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16
                    )),
                    SizedBox(height: 12),
                    ..._meetingData['agenda'].map((item) => Padding(
                      padding: EdgeInsets.symmetric(vertical: 8),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: 80,
                            child: Text(item['time'],
                                style: TextStyle(color: Colors.grey[600])),
                          ),
                          SizedBox(width: 16),
                          Expanded(
                            child: Text(item['topic'],
                                style: TextStyle(fontSize: 15)),
                          )
                        ],
                      ),
                    )).toList()
                  ],
                ),
              ),
            ),

            // 会议附件
            Card(
              child: Padding(
                padding: EdgeInsets.all(16),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('会议材料', style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16
                    )),
                    ..._meetingData['attachments'].map((file) => ListTile(
                      leading: Icon(
                        file['type'] == 'pdf'
                            ? Icons.picture_as_pdf
                            : Icons.insert_drive_file,
                        color: Colors.red,
                      ),
                      title: Text(file['name']),
                      subtitle: Text(file['size']),
                      trailing: IconButton(
                        icon: Icon(Icons.download),
                        onPressed: () {},
                      ),
                    )).toList()
                  ],
                ),
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: SafeArea(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
          child: Row(
            children: [
              Expanded(
                child: ElevatedButton.icon(
                  icon: Icon(Icons.videocam),
                  label: Text('立即加入'),
                  style: ElevatedButton.styleFrom(
                      padding: EdgeInsets.symmetric(vertical: 16),
                      backgroundColor: Colors.blue[600]
                  ),
                  onPressed: () {},
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildDetailCard({
    required IconData icon,
    required String title,
    required String content,
    Widget? trailing,
  }) {
    return Card(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Row(
          children: [
            Icon(icon, color: Colors.grey[600]),
            SizedBox(width: 16),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(title, style: TextStyle(
                      color: Colors.grey[600],
                      fontSize: 13
                  )),
                  SizedBox(height: 4),
                  Text(content,
                      style: TextStyle(fontSize: 15)),
                ],
              ),
            ),
            if (trailing != null) trailing,
          ],
        ),
      ),
    );
  }
}
